package util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeSplitUtil {
        /**
         * 以季度分割时间段
         * 此处季度是以 12-2月   3-5月   6-8月  9-11月 划分
         * @param startTime     开始时间戳(毫秒)
         * @param endTime       结束时间戳(毫秒)
         * @param beginDateList 开始段时间戳 和 结束段时间戳 一一对应
         * @param endDateList   结束段时间戳 和 开始段时间戳 一一对应
         */
        public static void getIntervalTimeByQuarter(Long startTime, Long endTime, List<Long> beginDateList, List<Long> endDateList) {
            Date startDate = new Date(startTime);
            Date endDate = new Date(endTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            int month = calendar.get(Calendar.MONTH) + 1;
            switch (month) {
                case 12:
                case 3:
                case 6:
                case 9:
                    addTime(beginDateList, endDateList, startDate, endDate, calendar, 3);
                    break;
                case 1:
                case 4:
                case 7:
                case 10:
                    addTime(beginDateList, endDateList, startDate, endDate, calendar, 2);
                    break;
                case 2:
                case 5:
                case 8:
                case 11:
                    addTime(beginDateList, endDateList, startDate, endDate, calendar, 1);
                    break;
            }
        }

        private static void addTime(List<Long> beginDateList, List<Long> endDateList, Date startDate, Date endDate, Calendar calendar, int i) {
            beginDateList.add(startDate.getTime());
            calendar.add(Calendar.MONTH, i);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.DATE, -1);
            calendar.set(Calendar.HOUR_OF_DAY, 13);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            if (calendar.getTimeInMillis() > endDate.getTime()) {
                endDateList.add(endDate.getTime());

            } else {
                endDateList.add(calendar.getTimeInMillis());
                while (calendar.getTimeInMillis() < endDate.getTime()) {
                    calendar.add(Calendar.DATE, 1);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    beginDateList.add(calendar.getTimeInMillis());
                    calendar.add(Calendar.MONTH, 3);
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    calendar.add(Calendar.DATE, -1);
                    calendar.set(Calendar.HOUR_OF_DAY, 13);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    if (calendar.getTimeInMillis() < endDate.getTime()) {
                        endDateList.add(calendar.getTimeInMillis());
                    } else {
                        endDateList.add(endDate.getTime());
                    }
                }
            }
        }

        /**
         * 以周分割时间段
         *
         * @param startTime     开始时间戳(毫秒)
         * @param endTime       结束时间戳(毫秒)
         * @param beginDateList 开始段时间戳 和 结束段时间戳 一一对应
         * @param endDateList   结束段时间戳 和 开始段时间戳 一一对应
         */
        public static void getIntervalTimeByWeek(Long startTime, Long endTime, List<Long> beginDateList, List<Long> endDateList) {
            Date startDate = new Date(startTime);
            Date endDate = new Date(endTime);
            SimpleDateFormat sdw = new SimpleDateFormat("E");
            Calendar calendar = Calendar.getInstance();
            String begin = sdw.format(startDate);
            calendar.setTime(startDate);
            beginDateList.add(calendar.getTimeInMillis());
            if ("星期一".equals(begin)) {
                addTimeStamp(beginDateList, endDateList, startDate, endDate, sdw, calendar);
            } else {
                if ("星期日".equals(sdw.format(startDate))) {
                    Calendar special = Calendar.getInstance();
                    special.setTime(startDate);
                    special.set(Calendar.HOUR_OF_DAY, 23);
                    special.set(Calendar.MINUTE, 59);
                    special.set(Calendar.SECOND, 59);
                    endDateList.add(special.getTime().getTime());
                }
                addTimeStamp(beginDateList, endDateList, startDate, endDate, sdw, calendar);
            }
        }

        private static void addTimeStamp(List<Long> beginDateList, List<Long> endDateList, Date startDate, Date endDate, SimpleDateFormat sdw, Calendar calendar) {
            while (startDate.getTime() < endDate.getTime()) {
                calendar.add(Calendar.DAY_OF_YEAR, 1);
                startDate = calendar.getTime();
                if ("星期一".equals(sdw.format(startDate))) {
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    beginDateList.add(calendar.getTimeInMillis());
                } else if ("星期日".equals(sdw.format(startDate)) || startDate.getTime() >= endDate.getTime()) {
                    if (startDate.getTime() <= endDate.getTime()) {
                        calendar.set(Calendar.HOUR_OF_DAY, 23);
                        calendar.set(Calendar.MINUTE, 59);
                        calendar.set(Calendar.SECOND, 59);
                        endDateList.add(calendar.getTimeInMillis());
                    } else {
                        endDateList.add(endDate.getTime());
                    }
                }
            }
        }

        /**
         * 按照月份分割一段时间
         *
         * @param startTime     开始时间戳(毫秒)
         * @param endTime       结束时间戳(毫秒)
         * @param beginDateList 开始段时间戳 和 结束段时间戳 一一对应
         * @param endDateList   结束段时间戳 和 开始段时间戳 一一对应
         */
        public static void getIntervalTimeByMonth(Long startTime, Long endTime, List<Long> beginDateList, List<Long> endDateList) {
            Date startDate = new Date(startTime);
            Date endDate = new Date(endTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            beginDateList.add(calendar.getTimeInMillis());
            while (calendar.getTimeInMillis() < endDate.getTime()) {
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.add(Calendar.DATE, -1);
                calendar.set(Calendar.HOUR_OF_DAY, 13);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                if(calendar.getTimeInMillis() < endDate.getTime()){
                    endDateList.add(calendar.getTimeInMillis());
                } else {
                    endDateList.add(endDate.getTime());
                    break;
                }
                calendar.add(Calendar.DATE, 1);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                beginDateList.add(calendar.getTimeInMillis());
            }
        }


}
